package com.miyake.client.datamanage;

import java.util.HashMap;
import java.util.Iterator;

import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.miyake.client.DataService;
import com.miyake.client.DataServiceAsync;
import com.miyake.client.datamanage.plugin.AnalysisPluginDialog;
import com.miyake.client.datamanage.plugin.PluginDialog;
import com.miyake.client.tools.OkCancelDialog;
import com.miyake.client.tools.ShowDialog;
import com.miyake.client.tools.SmallButton;

public abstract class ProcessDialog extends OkCancelDialog {

	private DataServiceAsync service = DataService.Util.getInstance();

	private HashMap<String, String> map = null;
	private String pluginKey;
	
	@Override
	protected Widget getContent() {
		setHTML("Data Processing");
		VerticalPanel panel = new VerticalPanel();
		panel.setWidth("100%");
		HorizontalPanel hpanel = new HorizontalPanel();
		ListBox listBox = new ListBox();
		hpanel.add(new Label("Target Column"));
		hpanel.add(listBox);
		
		for (Integer i = 1; i <= getColumnCount(); i++) {
			listBox.addItem("C" + i.toString());
		}
		panel.add(hpanel);

		final VerticalPanel custom = new VerticalPanel();
		panel.add(custom);
		
		final Label plugin = new Label();
		plugin.setWidth("300px");

		custom.add(plugin);
		
		final VerticalPanel paramPanel = new VerticalPanel();
		custom.add(new SmallButton("Select Service", new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				final PluginDialog dlg = new AnalysisPluginDialog();
				new ShowDialog() {

					@Override
					protected void onOK(String text) {
						plugin.setText(dlg.getSelectedName());
						pluginKey = dlg.getSelectedKey();
						paramPanel.clear();
						getAnalysisParameter(pluginKey, paramPanel);
					}

					@Override
					protected DialogBox getDialogBox() {
						return dlg;
					}
					
				};
			}			
		}));
		custom.add(paramPanel);

		return panel;
	}

	abstract protected Integer getColumnCount();

	protected void getAnalysisParameter(String key, final VerticalPanel custom) {
		service.getAnalysisParameters(getKey(), key, new AsyncCallback<AnalysisSetup>() {

			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onSuccess(AnalysisSetup result) {
				map = new HashMap<String, String>();
				for (AnalysisParameter p : result.getParameters()) {
					map.put(p.getName(), p.getValue());
				}
				custom.add(new Label(result.getName()));
				for (AnalysisParameter p : result.getParameters()) {
					HorizontalPanel panel = new HorizontalPanel();
					panel.add(new Label(p.getTitle()));
					if (p.getType().equals("Numeric") || p.getType().equals("String")) {
						final TextBox box = new TextBox();
						box.setName(p.getName());
						box.setText(p.getValue());
						panel.add(box);
						box.addChangeHandler(new ChangeHandler() {
							@Override
							public void onChange(ChangeEvent event) {
								map.put(box.getName(), box.getText());
							}
						});
					}
					else if (p.getType().equals("Selection")) {
						ListBox combo = new ListBox();
						for (String s : p.getSelection()) {
							combo.addItem(s);
						}
						panel.add(combo);
					}
					panel.add(new Label(p.getUnit()));
					custom.add(panel);
				}
			}
			
		});
	}

	abstract protected String getKey();
	
	public AnalysisSetup getSetup() {
		AnalysisSetup setup = new AnalysisSetup();
		Iterator<String> it = map.keySet().iterator();
		while(it.hasNext()) {
			AnalysisParameter p = new AnalysisParameter();
			String key = it.next();
			p.setName(key);
			p.setValue(map.get(key));
			setup.getParameters().add(p);
		}
		return setup;
	}

	public String getPluginKey() {
		return pluginKey;
	}
} 
